package com.example.owner.lettery1.ui

import android.content.Intent
import android.graphics.Color
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.TextView
import com.example.owner.lettery1.R
import com.example.owner.lettery1.base.KBaseActivity
import com.example.owner.lettery1.bean.Champion
import com.example.owner.lettery1.bean.LotteryBean
import com.example.owner.lettery1.modlus.SplitBall
import com.example.owner.lettery1.other.*
import com.example.owner.lettery1.utils.ResUtil


class ChampionActivity : KBaseActivity<ChampionPresenter>() {

    private var textTableView : TextTableView? = null
    private var gameInfoWebView : AdvancedWebView? = null
    private var labelsView : LabelsView? = null
    private var lotteryBean: LotteryBean? = null
    private var issueTextView: TextView? = null
    private var prizeDateTextView: TextView? = null
    private var totalView: TextView? = null
    private var poolTextView: TextView? = null


    override fun onInitView() {
        super.onInitView()
        addContentView(R.layout.activity_champion)

        textTableView = findViewById(R.id.textTableView)
        lotteryBean = intent.getSerializableExtra(LOTTERY_KEY) as LotteryBean
        textTableView!!.setHeaderHeight(130)
        poolTextView = findViewById(R.id.poolTextView)
        title = lotteryBean!!.lotteryName + "开奖情况"
        textTableView!!.setWeights(100, 100, 100)
        gameInfoWebView = findViewById(R.id.gameInfoWebView)
        gameInfoWebView!!.setRequestEvent(true)
        labelsView =findViewById(R.id.labelsView)
        issueTextView = findViewById(R.id.issueTextView)
        prizeDateTextView =findViewById(R.id.prizeDateTextView)
        totalView =findViewById(R.id.totalView)
    }

    override fun onCreatePresenter(): ChampionPresenter = ChampionPresenter()

    override fun onLoadData() {
        val observable = presenter!!.getChampionInfo(lotteryBean!!.lotteryNo!!, lotteryBean!!.issue!!)
        observable.subscribe({ data ->
            bindData(data)
        }, { t -> Log.e("ERROR",t.message) })
    }

    private fun bindData(champion: Champion) {
        //  championName.text = lotteryBean!!.lotteryName
        val ballList = SplitBall.splitBall(champion.award!!)
        labelsView!!.setLabels(ballList) { _, _, data -> data!!.number!! }
        if (lotteryBean!!.lotteryNo == "23528" || lotteryBean!!.lotteryNo == "23529" || lotteryBean!!.lotteryNo == "51") {
            val sections = ArrayList<Int>()
            for ((index, value) in ballList.withIndex()) {
                if (value.isSelector) {
                    sections.add(index)
                }
            }
            labelsView!!.setSelects(sections)
        } else if (lotteryBean!!.lotteryNo == "19") {
            labelsView!!.setLabelBackgroundResource(R.drawable.ball_red)
            labelsView!!.wordMargin = 5
        } else if (lotteryBean!!.lotteryNo == "11") {
            labelsView!!.setLabelBackgroundResource(R.drawable.ball_black)
            labelsView!!.wordMargin = 5
        } else if (lotteryBean!!.lotteryNo == "16") {
            labelsView!!.setLabelBackgroundResource(R.drawable.ball_green)
            labelsView!!.wordMargin = 5
        } else if (lotteryBean!!.lotteryNo == "18") {
            labelsView!!.setLabelBackgroundResource(R.drawable.ball_white)
            labelsView!!.wordMargin = 10
            labelsView!!.labelTextSize = 78f
            labelsView!!.setLabelTextColor(Color.rgb(255, 23, 68))
        } else if (lotteryBean!!.lotteryNo == "35" || lotteryBean!!.lotteryNo == "33" || lotteryBean!!.lotteryNo == "52") {
            labelsView!!.setLabelTextPadding(24, 6, 0, 0)
        }
        issueTextView!!.text = "第" + lotteryBean!!.issue + "期"
        prizeDateTextView!!.text = "开奖时间: " + champion.prizeDate
        totalView!!.text = champion.sale!!
        if (champion.pool == 0L) {
           // poolView.visibility = View.GONE
        } else {
            poolTextView!!.text = champion.pool.toString()
        }
        createForm(champion.prizeDetail)
        createGameInfo(lotteryBean!!.lotteryNo)
    }

    private fun createGameInfo(lotteryNo: String?) {
        when(lotteryNo!!){
            "19"-> gameInfoWebView!!.loadUrl(ResUtil.getString(R.string.local_js_file, "renxuan9"))
            "11"-> gameInfoWebView!!.loadUrl(ResUtil.getString(R.string.local_js_file, "sfgg"))
            "33" -> gameInfoWebView!!.loadUrl(ResUtil.getString(R.string.local_js_file, "pai3"))
            "35" -> gameInfoWebView!!.loadUrl(ResUtil.getString(R.string.local_js_file, "pai5"))
            "52" -> gameInfoWebView!!.loadUrl(ResUtil.getString(R.string.local_js_file, "fucai3D"))
            "23528" -> gameInfoWebView!!.loadUrl(ResUtil.getString(R.string.local_js_file, "qilecai"))
            "23529" -> gameInfoWebView!!.loadUrl(ResUtil.getString(R.string.local_js_file, "daletou"))
            "51" -> gameInfoWebView!!.loadUrl(ResUtil.getString(R.string.local_js_file, "doubleColor"))
            "10022" -> gameInfoWebView!!.loadUrl(ResUtil.getString(R.string.local_js_file, "qixingcai"))
            "16" -> gameInfoWebView!!.loadUrl(ResUtil.getString(R.string.local_js_file, "sfgg"))
        }
    }



    private fun createForm(prizeDetail: List<String>?) {
        if (prizeDetail!!.isEmpty()) return
        textTableView!!.setTitles("奖项", "中奖注数", "每注奖金(元)")
        val textTableAdapter = object : TextTableAdapter<String>(this) {
            override fun convert(holder: TextTableViewHolder, convertView: View, position: Int): View {
                //                    holder.setTexts("001", "宝宝护理", "护理部", "销售", "2015-11-11", "2015-11-13 13:28:51");
                val args = prizeDetail[position].split("_")
                holder.setTexts(args[0], args[2], args[1])
                val colors = arrayOf(Color.rgb(51, 51, 51), Color.rgb(51, 51, 51), Color.rgb(51, 51, 51))
                val sizes = arrayOf(14f, 14f, 14f)
                val backgrounds = arrayOf(R.color.white, R.color.white, R.color.white)
                holder.setTextStyle(colors, sizes, backgrounds)
                return convertView
            }
        }
        textTableAdapter.data = prizeDetail
        textTableView!!.setAdapter(textTableAdapter)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        if (item!!.itemId == R.id.menu_lottery_record) {
            val intent = Intent(this, ChampionListActivity::class.java)
            intent.putExtra(ChampionActivity.LOTTERY_KEY, lotteryBean)
            startActivity(intent)
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_record, menu)
        return super.onCreateOptionsMenu(menu)
    }

    companion object {
        val LOTTERY_KEY = "lottery_KEY"
        val BALL_KEY = "BALL_KEY"
    }
}

